<?php
class M_payroll_settings Extends MY_Model
{
	public function __construct()
	{
		parent::__construct();
	
	
	}
	
	public function set_payroll_settings($data)
	{
		$table = parent::$prs_table;
		$input['work_days'] = $data['wdim'];
		$input['work_hours'] = $data['whpd'];
		$input['work_out_pm'] = $data['stopm'];
		$input['work_in_pm'] = $data['stipm'];
		$input['work_in_am'] = $data['stoam'];
		$input['work_out_am'] = $data['stiam'];
		$input['minute_calc_overtime'] = $data['overtime'];
		$input['minimum_wage_rate'] = $data['mwr'];

		$query = $this->db->order_by('id','ASC')->get($table);
		
		if($query->num_rows() >=1 )
		{
			$this->db->set($input)->where('id',$query->row()->id)->update($table);
		}else{
			$this->db->insert($table,$input);
		}
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function get_mps()
	{	
		$table = parent::$prs_table;
		$query = $this->db->select('work_days as wdim,
									work_hours as whpd,
									work_out_am as stoam,
									work_out_pm as stopm,
									work_in_pm as stipm,
									work_in_am as stiam,
									minute_calc_overtime as overtime,
									minimum_wage_rate as mwr')->limit(1)->get($table);
		if($query->num_rows() >= 1)
		{
			return $query->row();
		}else{
		
			$input['wdim'] = '';
			$input['whpd'] = '';
			$input['stoam'] = '';
			$input['stopm'] = '';
			$input['stipm'] = '';
			$input['stiam'] = '';
			$input['overtime'] = '';
			$input['mwr'] = '';
		
		return (object)$input;

		}
	}
	
	public function get_data_for_salary_sss($x = FALSE)
	{
		$sss = "SELECT social_security_ee
				FROM ".parent::$sss_table."
				WHERE range_of_comp_from <= ?
				AND range_of_comp_to >= ?
				LIMIT 1";
				
		$sss_last = 'SELECT max(range_of_comp_from) as max_range,
							max(social_security_ee) as ssee
					FROM '.parent::$sss_table.' LIMIT 1';
		$max_sss = $this->db->query($sss_last);
		
		if($x >= $max_sss->row()->max_range)
		{
			return $max_sss->row()->ssee;
		}else{
			
			$query = $this->db->query($sss,array($x,$x));
			
			if($query->num_rows() >= 1)
			{
				return $query->row()->social_security_ee;
			}else{
				return FALSE;
			}

		}
	}
	
	public function get_data_for_salary_philhealth($x = FALSE)
	{
		$philhealth_max = "SELECT max(monthly_salary_range_from) as msrf,
								  max(employee_share) as es
						   FROM ".parent::$phh_table."
						   LIMIT 1 ";
						   
		$check_max = $this->db->query($philhealth_max);
		
		if($x >= $check_max->row()->msrf)
		{
			return $check_max->row()->es;
		}else{
			$philhealth = "SELECT employee_share
						   FROM ".parent::$phh_table." 
						   WHERE monthly_salary_range_from <= ?
						   AND monthly_salary_range_to >= ?
						   LIMIT 1 ";
						   
			$query = $this->db->query($philhealth,array($x,$x));

			if($query->num_rows() >= 1)
			{
				return $query->row()->employee_share;
			}else{
				return FALSE;
			}
		}
	
	}
}